<style scoped>

</style>

<template>
  <div>
    <el-row>
      <el-col :span="5">
        <div class="grid-content bg-purple">
          <el-row class="tac">
            <el-col :span="12">
              <side-menu></side-menu>
            </el-col>
          </el-row>
        </div>
      </el-col>
      <el-col :span="16">
        <div class="grid-content bg-purple-light">
          <el-form ref="form" :model="member" label-width="80px">
            <el-form-item label="昵称">
              <el-input v-model="member.nickname"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="member.mobile"></el-input>
            </el-form-item>

            <el-form-item label="密码修改" prop="passwordGroup">
              <el-input v-model="member.password" placeholder="原密码"></el-input>
            </el-form-item>
            <el-form-item label="密码修改" prop="passwordGroup">
              <el-input v-model="member.newPassWord" placeholder="新密码"></el-input>
            </el-form-item>

            <el-form-item label="性别">
              <el-select v-model="member.sex" placeholder="请选择性别">
                <el-option label="男" :value="2"></el-option>
                <el-option label="女" :value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="年纪">
              <el-input-number v-model="member.age" :min="0" :max="100" label="年纪"></el-input-number>
            </el-form-item>
            <el-form-item label="头像">
              <!-- 头衔缩略图 -->
              <pan-thumb :image="member.avatar"/>
              <!-- 文件上传按钮 -->
              <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像
              </el-button>
              <image-cropper
                v-show="imagecropperShow"
                :key="imagecropperKey"
                :url="BaseUrl"
                field="file"
                @close="close"
                @crop-upload-success="cropSuccess"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSubmit">确认修改</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col :span="3"></el-col>
    </el-row>
  </div>
</template>
<script>
import PanThumb from "../../components/PanThumb/index";
import ImageCropper from "../../components/ImageCropper/index";
import cookie from "js-cookie";
import {getInfo, updateInfo} from "../../api/member";
import SideMenu from "../../components/SideMenu/SideMenu";

export default {
  components: {SideMenu, PanThumb, ImageCropper},

  data() {
    return {
      imagecropperShow: false,
      imagecropperKey: 0,
      //上传地址
      // process.env.baseURL 不知道为什么这个结尾带未定义
      BaseUrl: 'http://localhost:9001/upload/avatar',
      member: {
        id: '',
        email: '',
        mobile: '',//手机号
        password: null,
        newPassWord: null,
        nickname: '',
        sex: '',
        age: '',
        avatar: 'https://hj-edu.oss-cn-wuhan-lr.aliyuncs.com/avatar/2024/06/01/75cd4c7b015441c399d081203f5aa9b7.jpg',
      },
      rules: {
        otherItem: [
          {required: true, message: '其他项不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    //查询个人信息回显
    getInfo() {
      let uid =  cookie.get('uid')
      getInfo(uid).then(
        res => {
          console.log(res)
          this.member = res.data
        }
      )
    },
    //修改个人信息
    updateInfo() {
      updateInfo(this.member).then(
        res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            this.getInfo()
          }
        }
      )
    },
    //上传成功回调方法
    cropSuccess(data) {
      this.member.avatar = data
      this.imagecropperShow = false
    },
    close() {
      this.imagecropperShow = false
      // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 验证通过，执行提交操作
          if ((this.member.password && this.member.newPassWord) || (!this.member.password && !this.member.newPassWord)) {
            // 这里是两个输入框要么同时有值，要么同时为空的情况
            // 执行提交操作
            console.log('提交成功');
            this.updateInfo()
          } else {
            this.$message.error('原密码和新密码必须同时有值，或者同时为空');
          }
        } else {
          return false;
        }
      });
    }

  },
  created() {
    this.getInfo();
  }
  //edu-front-ui/components/ImageCropper/index.vue  在848和233 屏蔽了两段js代码，不屏蔽此页面会有刷新bug
}
</script>

